<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use app\common\controller\AdminBaseController;

class Count extends AdminBaseController
{
	//商家信息统计
    public function agent_count()
    {
    	$list=Db::name('info_agent')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//统计
     public function summary()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
		}	
		$mems=db('info_user')->count();
		$agents=db('info_agent')->count();
    	$where['state']=2;
		$list=db("log_paylog")->where($where)->field("uid,balance,order_type,score,sy_type")->select();
		$data["mems"]=$mems;
		$data["agents"]=$agents;
		$data['mem_orders']=0;//会员消费订单笔数
		$data['mem_orders_ms']=0;//会员消费总额
		$data['sk_orders']=0;//散客消费订单笔数
		$data['sk_orders_ms']=0;//散客消费金额
		$data['sm_orders']=0;//扫码支付订单笔数
		$data['sm_orders_ms']=0;//扫码支付总额
		$data['glj_ms']=0;//鼓励金总额
		$data['sc_orders']=0;//商城订单
		$data['sc_orders_ms']=0;//商城订单金额
		if($list){
			foreach($list as $k=>$v){
				$data['glj_ms']+=$v['score'];
				if($v['uid']){
					$data['mem_orders']++;
					$data['mem_orders_ms']+=$v['balance'];
				}else{
					$data['sk_orders']++;
					$data['sk_orders_ms']+=$v['balance'];
				}
				 if($v['order_type']==1){
				 	$data['sc_orders']++;
					$data['sc_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==2){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==6&&$v['uid']){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }
			}
		}
		$mp=array('jz'=>$data['sk_orders_ms'],"sm"=>$data['sm_orders_ms'],'sc'=>$data['sc_orders_ms']);
		$this->assign('mp',json_encode($mp));
		$this->assign("data",$data);
    	return $this->fetch();
    }
	
    //OEM佣金记录
   	public function commission()
    {
    	if(request()->isPost()){   		
    		$param=input('post.');
			if(!empty($param['order_sn']))$where['order_sn'] = $param['order_sn'];			
    	}
		
		$where['state'] = 2;
		$where['member_score'] = ['>=',2];
    	$list=Db::name('log_paylog')
		->where($where)
		->order('add_time desc')
    	->paginate(15,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
		return $this->fetch();
    }

    //会员佣金记录
   	public function memcommission()
    {
		if(input('order_sn')){
			$where['a.order_sn'] = input('order_sn');
		}
		
		if(input('nickname')){
			$where['b.nickname'] = ['like','%'.input('nickname').'%'];	
		}
		
		$where['b.status'] = 1;
    	$list=Db::name('log_user_commission')    	
    	->alias('a')
    	->join('info_user b','a.u_id = b.id','LEFT')
    	->field('a.*,b.nickname')
		->where($where)
    	->paginate(15,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//佣金报表导出
    public function details_dc(){

		$where['a.o_id'] = session('d_id');
		$time="DATE_FORMAT(add_time,'%Y-%m-%d')";
    	$list=Db::table('log_oem_commission')->alias('a')
		    ->field('a.id,a.add_time,sum(a.balance) as balance')
		    ->group($time."desc")
		    ->select();
		

		$str ="佣金,时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$balance = iconv('utf-8','gbk',$row['balance'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $balance.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='佣金报表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

	//明细查询
    public function detailed()
    {
    	if(input('oemname'))$where['a.oem_id'] = input('oemname');
		if(input('sdname'))$where['a.agency_id1'] = input('sdname');
		if(input('sdlist'))$where['a.agency_id2'] = input('sdlist');
		if(input('ywylist'))$where['a.salesman_id'] = input('ywylist');
		if(input('dplist'))$where['a.agent_id'] = input('dplist');
		if(input('storelist'))$where['a.chain_id'] = input('storelist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

		$where['a.order_type'] = ['not in','3,4,5'];	
    	$paylog = db('log_paylog')
    	->alias('a')
    	->join('info_agent b','a.agent_id = b.id','LEFT')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->field('a.*,b.agent_name,w.store_name')
    	->where($where)
		->order('a.add_time desc')
    	->paginate(15,false,['query'=>request()->param()]);
		$this->assign('paylog',$paylog);
		
		//获取OEM
		$oem = db('info_oem')->where('user_level',1)->where('status',1)->select();
		$this->assign('oem',$oem);
		
		//获取省代
		$info_oem = db('info_oem')->where('user_level',2)->where('status',1)->select();
		$this->assign('info_oem',$info_oem);
		
		//消费金额
		$xfprice = db('log_paylog')->alias('a')->where($where)->sum('a.origin_price');
		$this->assign('xfprice',$xfprice);
		
		//实际金额
		$sjprice = db('log_paylog')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sjprice',$sjprice);
		
		//赠送
		$zsscore = db('log_paylog')->alias('a')->where($where)->where('a.payment_type',1)->sum('a.score');
		$this->assign('zsscore',$zsscore);
		
		//抵扣
		$dkscore = db('log_paylog')->alias('a')->where($where)->where('a.payment_type',2)->sum('a.score');
		$this->assign('dkscore',$dkscore);
		
		//获取门店ID
		$agent_store = db('agent_store')->where('agent_id',session('agent_id'))->select();
		$this->assign('agent_store',$agent_store);
		
		//获取收银员
		$clerk_list = db('agent_clerk')->where('agent_id',session('agent_id'))->select();
		$this->assign('clerk_list',$clerk_list);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }

	//明细导出
    public function detail_dc(){
    	if(input('oemname'))$where['a.oem_id'] = input('oemname');
		if(input('sdname'))$where['a.agency_id1'] = input('sdname');
		if(input('sdlist'))$where['a.agency_id2'] = input('sdlist');
		if(input('ywylist'))$where['a.salesman_id'] = input('ywylist');
		if(input('dplist'))$where['a.agent_id'] = input('dplist');
		if(input('storelist'))$where['a.chain_id'] = input('storelist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('sy_type'))$where['a.sy_type'] = input('sy_type');
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

		$where['a.order_type'] = ['not in','3,4,5'];	
    	$list = db('log_paylog')
    	->alias('a')
    	->join('info_agent b','a.agent_id = b.id','LEFT')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->field('a.*,b.agent_name,w.store_name')
    	->where($where)
		->order('a.add_time desc')
    	->select();

		$str ="订单号,门店,项目,支付方式,消费金额,实际金额,鼓励金,日期"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$agent_name = iconv('utf-8','gbk',$row['agent_name']); 
			$store_name = iconv('utf-8','gbk',$row['store_name']); 
			if($row['sy_type']==1){
				$syname = '记账';
			}elseif($row['sy_type']==2) {
				$syname = '扫码';
			}else{
				$syname = '商品';
			}
			$syname = iconv('utf-8','gbk',$syname); 
			
			if($row['pay_type']==1){
				$payname = '余额';
			}elseif($row['pay_type']==2) {
				$payname = '微信';
			}elseif($row['pay_type']==3) {
				$payname = '支付宝';
			}else{
				$payname = '银联';
			}
			$pay_type = iconv('utf-8','gbk',$payname);
			$xfprice = iconv('utf-8','gbk','	'.$row['origin_price']);
			$sjprice = iconv('utf-8','gbk','	'.$row['balance']);
			if($row['payment_type']==1){
				$oprate = '+';
			}elseif($row['payment_type']==2){
				$oprate = '-';
			}else{
				$oprate = '';
			}			
			$score = iconv('utf-8','gbk','	'.$oprate.$row['score']);
			$str .= $order_sn.",".$agent_name.'/'.$store_name.",".$syname.",".$pay_type.",".$xfprice.",".$sjprice.",".$score.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='明细列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

    public function sdname()
    {
    	//获取省代
		$data = db('info_oem')->where('oem_id',input('id'))->where('user_level',2)->where('status',1)->select();
		return json(["data"=>$data]);
    } 

    public function sdlist()
    {
    	//获取市代
		$data = db('info_oem')->where('agency_id',input('id'))->where('user_level',3)->where('status',1)->select();
		return json(["data"=>$data]);
    } 

    public function ywylist()
    {
    	//获取业务员
		$data = db('info_salesman')->where('agency_id2',input('id'))->select();
		//获取店铺
		$datas = db('info_agent')->where('agency_id2',input('id'))->select();
		return json(["data"=>$data,"datas"=>$datas]);
    } 

    public function storelist()
    {
    	//获取店铺所属门店
		$data = db('agent_store')->where('agent_id',input('id'))->select();
		return json(["data"=>$data]);
    } 
	//平台鼓励金
    public function ptscore_count()
    {
    	$totals=Db::name('admin_charge')->sum("balance");
		$this->assign('totals',$totals);
		$lists=Db::name('admin_charge')->paginate(10);
    	$this->assign('lists',$lists);
    	return $this->fetch();
    }
	//会员鼓励金
    public function memscore_count()
    {
		$list=Db::name('log_user_score')
		->alias('a')
    	->join('info_user b','a.u_id = b.id','LEFT')
    	->field('a.*,b.id,b.mobile')
    	->paginate(10);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	//导出数据
	public function score_dc(){
		$type=input("type");
    	if($type){
    		$where['a.order_type']=$type;
			
    	}else{
    		$type=0;
    		$where['a.order_type']=['in','8,5,9'];
    	}
		if(input("mobile")){
			if($type==8){
				$where['c.mobile']=input('mobile');
			}elseif($type==5){
				$where['d.mobile']=input('mobile');
			}elseif($type==9){
				$where['b.mobile']=input('mobile');
			}
			$this->assign('mobile',input("mobile"));
		}
		$this->assign('type',$type);
		if(input("begin_time")){
			$where['a.add_time']=['>=',input("begin_time")];
			$this->assign('begin_time',input("begin_time"));
		}
		if(input("end_time")){
			$where['a.add_time']=['<=',input("end_time")];
			$this->assign('end_time',input("end_time"));
		}
		$list=db('log_paylog_copy')->alias('a')
		->join('info_oem b','a.uid = b.id','LEFT')
    	->join('info_user c','a.uid = c.id','LEFT')
		->join('info_agent d','a.agent_id = d.id','LEFT')->where($where)
    	->field('a.*,b.user_name,b.mobile as mobile,c.user_name as name2,c.mobile as mobile2,d.agent_name,d.mobile as mobile3')
    	->select();

		$str ="订单号,用户类型,手机号码,名称,描述,支付方式,金额,日期"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			if($row['order_type']==8){
				$user_type='会员';
				$name=$row['user_name'];
				$mobile=$row['mobile2'];
			}elseif($row['order_type']==5){
				$user_type='商家';
				$name=$row['agent_name'];
				$mobile=$row['mobile3'];
			}else{
				$user_type='服务商';
				$name=$row['user_name'];
				$mobile=$row['mobile'];
			}
			$pay_tpye="微信";
			if($row['pay_type']==3){
				$pay_tpye=="支付宝";
			}
			$user_type=iconv('utf-8','gbk',$user_type);
			$name=iconv('utf-8','gbk',$name);
			$mobile=iconv('utf-8','gbk',$mobile);
			$message=iconv('utf-8','gbk',$row['message']);
			$pay_tpye=iconv('utf-8','gbk',$pay_tpye);
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $order_sn.",".$user_type.",".$mobile.",".$name.",".$message.",".$pay_tpye.",".$row['balance'].",".$row['add_time']."\n"; //用引文逗号分开 
        } 

	  	$filename ='鼓励金充值报表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	//鼓励金
    public function score_count(){
    	$type=input("type");
    	if($type){
    		$where['a.order_type']=$type;
    	}else{
    		$type=0;
    		$where['a.order_type']=['in','8,5,9'];
    	}
		if(input("mobile")){
			if($type==8){
				$where['c.mobile']=input('mobile');
			}elseif($type==5){
				$where['d.mobile']=input('mobile');
			}elseif($type==9){
				$where['b.mobile']=input('mobile');
			}
			$this->assign('mobile',input("mobile"));
		}
		$this->assign('type',$type);
		if(input("begin_time")){
			$where['a.add_time']=['>=',input("begin_time")];
			$this->assign('begin_time',input("begin_time"));
		}
		if(input("end_time")){
			$where['a.add_time']=['<=',input("end_time")];
			$this->assign('end_time',input("end_time"));
		}
		$list=db('log_paylog_copy')->alias('a')
		->join('info_oem b','a.uid = b.id','LEFT')
    	->join('info_user c','a.uid = c.id','LEFT')
		->join('info_agent d','a.agent_id = d.id','LEFT')->where($where)
    	->field('a.*,b.user_name,b.mobile as mobile,c.user_name as name2,c.mobile as mobile2,d.agent_name,d.mobile as mobile3')
    	->paginate(10,false,['query'=>request()->param()]);

		$sums=db('log_paylog_copy')->alias('a')
			->join('info_oem b','a.uid = b.id','LEFT')
	    	->join('info_user c','a.uid = c.id','LEFT')
			->join('info_agent d','a.agent_id = d.id','LEFT')->where($where)
	    	->sum("a.balance");
		$this->assign('sums',$sums);
    	$this->assign('list',$list);
		return $this->fetch();
    }
	
	//广告费
    public function advert_count()
    {
    	if(request()->isPost()){   		
    		$param=input('post.');
			if(!empty($param['agent_name']))$where['b.agent_name'] = ['like','%'.$param['agent_name'].'%'];		
    	}

		$where['a.order_type']=['in','7,8'];
    	$list=Db::name('log_agent_charge')->alias('a')
    	->join('info_agent b','a.agent_id = b.id','LEFT')
    	->field('a.*,b.agent_name')
    	->where($where)
    	->paginate(10,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//会员统计
    public function user_count()
    {
    	$list=Db::table('info_user')->order('add_time desc')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//商家消费记录
    public function charge_count()
    {
    	$where='';
		if(input('agent_name')){
			$where['b.agent_name'] = ['like','%'.input('agent_name').'%'];	
		}

    	$list=Db::name('log_agent_charge')    	
    	->alias('a')
    	->join('info_agent b','a.agent_id = b.id')
    	->field('a.*,b.agent_name')
		->where($where)
		->order('a.add_time desc')
    	->paginate(15,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }

	//会员消费记录
    public function memcharge_count()
    {
    	$where='';
		if(input('nickname')){
			$where['b.nickname'] = ['like','%'.input('nickname').'%'];	
		}
		
		$list=Db::name('log_user_charge')
		->alias('a')
    	->join('info_user b','a.u_id = b.id','LEFT')
    	->field('a.*,b.nickname')
		->where($where)
		->order('a.add_time desc')
    	->paginate(15,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
}
